package com.fzk.router;

import io.netty.handler.codec.http.HttpMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器映射
 *
 * @author fzk
 * @datetime 2023-04-15 01:54:56
 */
public class InterceptorMapping {
    private final ArrayList<MatchRule> rules = new ArrayList<>();
    public final Interceptor interceptor;

    public InterceptorMapping(List<String> paths, List<HttpMethod> methods,Interceptor interceptor) {
        if (paths == null || paths.isEmpty() || methods == null || methods.isEmpty()) {
            throw new RuntimeException("不能为空");
        }
        for (String p : paths) {
            // 直接加入, 不判断冗余或重复
            rules.add(MatchRule.of(p, methods));
        }
        this.interceptor=interceptor;
    }

    public boolean match(String path, HttpMethod method) {
        if (rules.isEmpty()) {
            throw new RuntimeException("需添加匹配规则");
        }
        for (MatchRule matchRule : rules) {
            if (matchRule.match(path, method)) {
                return true;
            }
        }
        return false;
    }
}
